﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Presentation
{
    public partial class UserControlSetKeyOutput : UserControl
    {
        public UserControlSetKeyOutput()
        {
            InitializeComponent();
            _InputAuto = new BUS.CInputKeyRunningAuto();
            _InputManual = new BUS.CInputKeyRunningManual();
            _UndoRedo = new BUS.CUndoRedo();
            _NhanKey = false;
            _InputManual.Install(ref rtxtKeyRunning);
            _UndoRedo.Install(ref rtxtKeyRunning);
        }

        #region

        private BUS.CInputKeyRunningAuto _InputAuto;
        private BUS.CInputKeyRunningManual _InputManual;
        private BUS.CUndoRedo _UndoRedo;

        private Boolean _NhanKey;
        public Boolean InputAuto
        {
            get { return _NhanKey; }
            set
            {
                if (_NhanKey != value)
                {
                    _NhanKey = value;
                    ChuyenCheDo();
                }
            }
        }


        public string KeyRunning
        {
            get { return rtxtKeyRunning.Text; }
            set { rtxtKeyRunning.Text = value; _UndoRedo.Reset(rtxtKeyRunning); }
        }

        #endregion

        #region event

        private void UserControlSetKeyOutput_Load(object sender, EventArgs e)
        {
        }


        /// <summary>
        /// Thay đổi kích thước các control bên trong khi thay đổi kích thước control chính
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControlSetKeyOutput_Resize(object sender, EventArgs e)
        {
            if(_NhanKey)
                rtxtKeyRunning.Height = Height - plbbcodeMain.Height/2 - 10;
            else
                rtxtKeyRunning.Height = Height - plbbcodeMain.Height - 10;
        }

        #region Tool


        /// <summary>
        /// Chuyển chế độ nhập khi bấm vào bbCode Change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uscbbcodeChange_LClick(object sender, MouseEventArgs e)
        {
            InputAuto = !InputAuto;
            rtxtKeyRunning.Focus();
        }

        private void nudSize_ValueChanged(object sender, EventArgs e)
        {
            Font _font = new Font(rtxtKeyRunning.Font.Name, (float)nudSize.Value, rtxtKeyRunning.Font.Style, rtxtKeyRunning.Font.Unit);
            rtxtKeyRunning.Font = _font;
        }

        private void ucbbcodeClead_LClick(object sender, MouseEventArgs e)
        {
            rtxtKeyRunning.Clear();
            rtxtKeyRunning.Focus();
        }

        private void ucbbcodeDel_LClick(object sender, MouseEventArgs e)
        {
            BUS.CDeleteCode.DeleteCode(ref rtxtKeyRunning);
            rtxtKeyRunning.Focus();
        }

        private void ucbbcodeUndo_LClick(object sender, MouseEventArgs e)
        {
            _UndoRedo.Undo(ref rtxtKeyRunning);
            rtxtKeyRunning.Focus();
        }

        private void ucbbcodeRedo_LClick(object sender, MouseEventArgs e)
        {
            _UndoRedo.Redo(ref rtxtKeyRunning);
            rtxtKeyRunning.Focus();
        }

        private void ucbbcodeCtrl_LClick(object sender, MouseEventArgs e)
        {
            BUS.CInsertCode.AddString(ref rtxtKeyRunning, BUS.CTransferKeysCode.SwithText(Keys.ControlKey));
            rtxtKeyRunning.Focus();
        }

        private void ucbbcodeAlt_LClick(object sender, MouseEventArgs e)
        {
            BUS.CInsertCode.AddString(ref rtxtKeyRunning, BUS.CTransferKeysCode.SwithText(Keys.Menu));
            rtxtKeyRunning.Focus();
        }

        private void ucbbcodeShift_LClick(object sender, MouseEventArgs e)
        {
            BUS.CInsertCode.AddString(ref rtxtKeyRunning, BUS.CTransferKeysCode.SwithText(Keys.ShiftKey));
            rtxtKeyRunning.Focus();
        }

        private void userControlBBCode1_LClick(object sender, MouseEventArgs e)
        {
            SetDelay _setdelay=new SetDelay();
            int _temp;
            _temp=_setdelay.ShowSetDelay();
            if(_temp!=-1)
            {
                BUS.CInsertCode.AddDelay(ref rtxtKeyRunning, _temp);
            }
            rtxtKeyRunning.Focus();
        }

        #endregion

        #endregion

        #region

        /// <summary>
        /// Thay đổi giao diện và 1 số thiết lập khi chuyển chế độ nhập
        /// </summary>
        private void ChuyenCheDo()
        {
            if (!_NhanKey)
            {
                _InputAuto.Uninstall(ref rtxtKeyRunning);
                _InputManual.Install(ref rtxtKeyRunning);
                uscbbcodeChange.ColorNormal = Color.Transparent;
                rtxtKeyRunning.BackColor = Color.White;
                rtxtKeyRunning.BorderStyle = BorderStyle.FixedSingle;
                uscbbcodeChange.BorderStyle = BorderStyle.None;
                plbbcodeMain.Height += flpbbcodeTuCode.Height;
                rtxtKeyRunning.Height -= flpbbcodeTuCode.Height;
                rtxtKeyRunning.Location = new Point(rtxtKeyRunning.Location.X, rtxtKeyRunning.Location.Y + flpbbcodeTuCode.Height);
                rtxtKeyRunning.AcceptsTab = true;
            }
            else
            {
                _InputAuto.Install(ref rtxtKeyRunning);
                _InputManual.Uninstall(ref rtxtKeyRunning);
                uscbbcodeChange.ColorNormal = Color.FromArgb(200, 241, 246, 248);
                rtxtKeyRunning.BackColor = Color.FromArgb(241, 246, 248);
                rtxtKeyRunning.BorderStyle = BorderStyle.Fixed3D;
                uscbbcodeChange.BorderStyle = BorderStyle.FixedSingle;
                plbbcodeMain.Height -= flpbbcodeTuCode.Height;
                rtxtKeyRunning.Height += flpbbcodeTuCode.Height;
                rtxtKeyRunning.Location = new Point(rtxtKeyRunning.Location.X, rtxtKeyRunning.Location.Y - flpbbcodeTuCode.Height);
                rtxtKeyRunning.AcceptsTab = true;
            }
        }

        #endregion

        private void bbcodeCheck_LClick(object sender, MouseEventArgs e)
        {
            if (!BUS.CCheck.CheckRuningKey(rtxtKeyRunning.Text))
                MessageBox.Show("RunnningKey error! Please edit or input new RunningKey!!","Waning",MessageBoxButtons.OK);
            BUS.CCorlorRichTextBox.Highlight(rtxtKeyRunning);
            rtxtKeyRunning.Focus();
        }

        private void bbcodeView_LClick(object sender, MouseEventArgs e)
        {
            AllForm.ViewKeys _frmview = new AllForm.ViewKeys(rtxtKeyRunning.Text);
            _frmview.ShowDialog();
        }

    }
}